package com.mudongheng.app.config;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
 * @author 幕冬
 * @since 2022年03月09日
 */
@Configuration
public class MyBatisPlusConfig {

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
        configurer.setBasePackage("com.mudongheng.app.mapper");
        return configurer;
    }

    @Bean
    public DriverManagerDataSource dataSourceProperties() {
        DriverManagerDataSource res = new DriverManagerDataSource();
        res.setDriverClassName("com.mysql.cj.jdbc.Driver");
        res.setUrl("jdbc:mysql://localhost:3306/test");
        res.setUsername("root");
        res.setPassword("123456");
        return res;
    }

    @Bean
    public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean(DriverManagerDataSource dataSource) {
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(dataSource);
        return mybatisSqlSessionFactoryBean;
    }
}
